import logging
import time

from selenium.webdriver.support.wait import WebDriverWait


from utils import DriverUtils


class WebBasePage:
    # 元素定位
    def find_element(self, locator):
        try:
            el = (WebDriverWait(DriverUtils.get_web_driver(),10,1).
              until(lambda x:x.find_element(*locator)))
            logging.info(f"find {locator} success")
        except Exception as e:
            el =  None
            logging.error(f"find {locator} failed")
        return el
    # 输入
    def input_text(self,element,text):
        try:
            element.clear()
            element.send_keys(text)
            logging.info(f"input {text} success")
        except Exception as e:
            logging.error(f"input {text} failed")
    # 点击
    def click_element(self,element):
        try:
            element.click()
            logging.info(f"click {element} success")
        except Exception as e:
            logging.error(f"click {element} failed")
    # frame切换
    def switch_frame(self,frame_element):
        try:
            DriverUtils.get_web_driver().switch_to.frame(frame_element)
            logging.info(f"switch frame {frame_element} success")
        except Exception as e:
            logging.error(f"switch frame {frame_element} failed")
    # 窗口切换
    def switch_window(self,n):
        try:
            # 获取所有窗口句柄
            handles = DriverUtils.get_web_driver().window_handles
            # 切换窗口
            DriverUtils.get_web_driver().switch_to.window(handles[n])
            logging.info(f"switch window {handles[n]} success")
        except Exception as e:
            logging.error(f"switch window {handles[n]} failed")
class AppBasePage:
    # 元素定位
    def find_element(self, locator):
        try:
            el = (WebDriverWait(DriverUtils.get_app_driver(),10,1).
              until(lambda x:x.find_element(*locator)))
            logging.info(f"find {locator} success")
        except Exception as e:
            el =  None
            logging.error(f"find {locator} failed")
        return el
    # 输入
    def input_text(self,element,text):
        try:
            element.clear()
            element.send_keys(text)
            logging.info(f"input {text} success")
        except Exception as e:
            logging.error(f"input {text} failed")
    # 点击
    def click_element(self,element):
        try:
            element.click()
            logging.info(f"click {element} success")
        except Exception as e:
            logging.error(f"click {element} failed")
    # frame切换
    def switch_frame(self,frame_element):
        try:
            DriverUtils.get_app_driver().switch_to.frame(frame_element)
            logging.info(f"switch frame {frame_element} success")
        except Exception as e:
            logging.error(f"switch frame {frame_element} failed")
    # 窗口切换
    def switch_window(self,n):
        try:
            # 获取所有窗口句柄
            handles = DriverUtils.get_app_driver().window_handles
            # 切换窗口
            DriverUtils.get_web_driver().switch_to.window(handles[n])
            logging.info(f"switch window {handles[n]} success")
        except Exception as e:
            logging.error(f"switch window {handles[n]} failed")



